using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Oops.Xna.Framework.Graphics
{
	/// <summary>
	/// Maintains the current animation state of a <see cref="SkinningData"/> instance by using
	/// the GPU.
	/// </summary>
	public class GpuAnimationPlayer : AnimationPlayer 
	{
		private float animationFrame;

		/// <summary>
		/// Initializes a new instance of the <see cref="GpuAnimationPlayer"/> class.
		/// </summary>
		/// <param name="skinnedData">The associated <see cref="SkinningData"/>.</param>
		public GpuAnimationPlayer(SkinningData skinnedData)	: base(skinnedData) { }

		/// <summary>
		/// Gets the current animation frame of the <see cref="GpuAnimationPlayer"/>.
		/// </summary>
		public float AnimationFrame
		{
			get { return this.animationFrame; }
		}

		#region AnimationPlayer Members

		public override void AdjustEffect(Effect effect)
		{
			// Assign to effect shader constants.
			effect.Parameters["AnimationTexture"].SetValue(this.SkinningData.AnimationTexture);
			effect.Parameters["AnimationFrame"].SetValue(this.AnimationFrame);

			// Call inherited method.
			base.AdjustEffect(effect);
		}

		protected override void OnCurrentTimeChanged(EventArgs e)
		{
			// Update the current animation frame.
			this.animationFrame = 
				this.CurrentClip.StartRow + 
				(float)this.CurrentTime.TotalSeconds * this.CurrentClip.FrameRate;

			// Call inherited method.
			base.OnCurrentTimeChanged(e);
		}

		#endregion
	}
}